const { merge } = require('webpack-merge');
const singleSpaDefaults = require('webpack-config-single-spa-react');
const config = require('./config');
var path = require('path');

module.exports = (webpackConfigEnv, argv) => {
  const defaultConfig = singleSpaDefaults({
    orgName: 'mf',
    projectName: config.projectName,
    webpackConfigEnv,
    argv,
  });

  return merge(defaultConfig, {
    // modify the webpack config however you'd like to by adding to this object
    entry: ['./src/index.js'],
    output: {
      filename: 'app.js',
    },
    devServer: {
      client: {
        overlay: {
          runtimeErrors: false, // 禁用运行时错误遮罩(webpack 5+)
        },
      },
      port: config.port,
      proxy: {
        '/grc_coreserv': {
          target: 'http://106.53.176.56:8080',
          changeOrigin: true,
        },
      },
    },
    module: {
      rules: [
        {
          test: /\.less$/i,
          use: [
            'style-loader',
            {
              loader: 'css-loader',
              options: {
                modules: {
                  mode: 'local',
                  auto: true,
                  localIdentName: '[local]--[hash:base64:5]',
                },
              },
            },
            {
              loader: 'less-loader',
              options: {
                lessOptions: {
                  modifyVars: {
                    '@ant-prefix': 'isc', // 特别命名的类名前缀可做到应用之间的样式隔离
                  },
                  javascriptEnabled: true,
                },
              },
            },
          ],
        },
        {
          test: /\.(png|jpe?g|gif|svg|webp)$/i,
          type: 'asset/resource',
          generator: {
            filename: 'static/images/[name].[hash:8][ext]', // 图片输出路径和命名规则
          },
        },
        // 添加字体文件加载规则
        {
          test: /\.(woff|woff2|eot|ttf|otf)$/i,
          type: 'asset/resource',
          generator: {
            filename: 'static/fonts/[name].[hash:8][ext]', // 字体文件输出路径和命名规则
          },
        },
      ],
    },
    // externals: webpackConfigEnv.standalone ? [] : ['react', 'react-dom'],
    devtool:
      // eslint-disable-next-line no-undef
      process.env.GENERATE_SOURCEMAP !== 'false' ? 'cheap-module-source-map' : false,
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        ...(webpackConfigEnv.standalone
          ? {
              react: path.resolve('./node_modules/react'),
              'react-dom': path.resolve('./node_modules/react-dom'),
            }
          : {}),
      },
    },
  });
};
